package com.ruoyi.exam.controller;

import java.util.List;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.exam.dto.ExamPaperDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.ExamPaper;
import com.ruoyi.exam.service.IExamPaperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷信息Controller
 *
 * @author wangshi
 * @date 2021-10-25
 */
@RestController
@RequestMapping("/exam/paper")
public class ExamPaperController extends BaseController
{
    @Autowired
    private IExamPaperService examPaperService;

    /**
     * 查询试卷信息列表
     */
    @PreAuthorize("@ss.hasPermi('exam_system:paper:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPaper examPaper)
    {
        startPage();
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        return getDataTable(list);
    }

    /**
     * 导出试卷信息列表
     */
    @PreAuthorize("@ss.hasPermi('exam_system:paper:export')")
    @Log(title = "试卷信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ExamPaper examPaper)
    {
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        ExcelUtil<ExamPaper> util = new ExcelUtil<ExamPaper>(ExamPaper.class);
        return util.exportExcel(list, "试卷信息数据");
    }

    /**
     * 获取试卷信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam_system:paper:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(examPaperService.selectExamPaperById(id));
    }

    /**
     * 新增试卷信息
     */
    @PreAuthorize("@ss.hasPermi('exam_system:paper:add')")
    @Log(title = "试卷信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPaperDto examPaperDto)
    {
        // 获取登录人ID
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        examPaperDto.setUserId(userId);
        examPaperDto.setDel("0");
        return toAjax(examPaperService.insertExamPaper(examPaperDto));
    }

    /**
     * 修改试卷信息
     */
    @PreAuthorize("@ss.hasPermi('exam_system:paper:edit')")
    @Log(title = "试卷信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPaper examPaper)
    {
        return toAjax(examPaperService.updateExamPaper(examPaper));
    }

    /**
     * 删除试卷信息
     */
    @PreAuthorize("@ss.hasPermi('exam_system:paper:remove')")
    @Log(title = "试卷信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(examPaperService.deleteExamPaperByIds(ids));
    }
}
